'use strict'

const playerHandle = require('../core/playerHandle')
const route = require('../module/route')
const playerSessionMgr = require('../core/playerSessionMgr')
const attackHandle = require('../core/attackHandle')
const Monster = require('../component/monster')
const SUN = require('../base/sun')
const ItemComponent = require('../component/itemComponent')
let mapConfig = SUN.totalConfig.map
let npcConfig = SUN.totalConfig.npc
let taskConfig = SUN.totalConfig.task
let itemConfig = SUN.totalConfig.item

//查看邮箱
route.set('getMails', function(player, msg, next) {
    next(0)
})

//查看邮件
route.set('getMail', function(player, msg, next) {
    next(0)
})

//提取邮箱东西
route.set('extractMail', function(player, msg, next) {
    next(0)
})

//删除邮件
route.set('deleteMail', function(player, msg, next) {
    next(0)
})

//查看背包
route.set('getBagInfo', function(player, msg, next) {
    next(0, player.bag)
})

//删除背包东西
route.set('delItem', function(player, msg, next) {
    let ret = player.bag.removeItemByPosition(msg.pos)
    next(ret)
}, {1: '删除的背包格子没有物品'})

//添加物品
route.set('addItem', function(player, msg, next) {
    let itemId = msg.itemId
    let stack = msg.stack
    let ret = SUN.verification(msg, [
        [
            stack,
            'number', {
                max: 99,
                min: 1
            }
        ]
    ])
    if (ret) {
        next(1, ret)
        return
    }

    if (itemConfig[itemId]) {
        let itemComponent = ItemComponent.getPropComponent({templateId: itemId, stack: stack})
        let ret = player.bag.addItem(itemComponent)
        next(0)
    } else {
        next(3)
    }
}, {3: '物品id不存在'})

//整理背包
route.set('arrange', function(player, msg, next) {
    player.bag.arrange()
    next(0, player.bag)
})

//获取角色信息
route.set('getPlayerInfo', function(player, msg, next) {
    next(0, player)
})

//保存角色信息
route.set('savePlayer', function(player, msg, next) {
    player.save(function(err) {
        if (err) {
            next(101)
        } else {
            next(0)
        }
    })
})

//查看我的任务列表
route.set('getPlayerTask', function(player, msg, next) {
    next(0, player.task)
})
